package cn.etg.core;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/*
webserver 服务端
 */
public class WebServerApplication {
    private ServerSocket serverSocket;
    private ExecutorService threadPool;

    public WebServerApplication(){
        System.out.println("启动服务端...");
        try {
            serverSocket = new ServerSocket(8080);
            threadPool = Executors.newFixedThreadPool(50);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("服务器已经启动");
    }

    public void start(){
        try {
            while (true){
                System.out.println("等待客户端连接");
                Socket socket = serverSocket.accept();
                System.out.println("客户端已连接");
                // 启动一个线程处理与客户端的交互
                ClientHandler clientHandler = new ClientHandler(socket);
                threadPool.execute(clientHandler);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    public static void main(String[] args) {
        WebServerApplication server = new WebServerApplication();
        server.start();
    }
}
